﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;

namespace CRMBiz
{
    public static class BusinessLogoHandler
    {

        /// <summary>
        /// function for get logo list
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static DataSet GetLogoList(int pageIndex, int pageSize, string Company)
        {

            DataSet ds;
            try
            {
                var sbSql = new StringBuilder("SELECT ID,ImageFileName,Description,if (Company='null','',Company) as  CompanyName,(select count(*) FROM ")
                .AppendFormat(" crm_logo where Company LIKE '%{0}%') as Total from crm_logo where Company LIKE '%{0}%' order by ID", Company)
                .Append(" LIMIT ").Append((pageIndex - 1) * pageSize).Append(", ").Append(pageSize);
                ds = DBhelper.ExecuteDataset(sbSql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        /// <summary>
        /// function for add business logo
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int AddBusinessLogo(string ImageFileName, string Company, string Description)
        {
            int intReturnValue = 0;
            DataSet ds = new DataSet();
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ImageFileName", ImageFileName),
                    DBhelper.SetParameter("@Company", Company),
                    DBhelper.SetParameter("@Description", Description),
                };
                ds = DBhelper.ExecuteDataset("Select * From crm_logo Where Company = @Company", para);

                if (ds.Tables[0].Rows.Count > 0)
                    intReturnValue = 0;
                else
                {
                    DBhelper.ExecuteNonQuery("INSERT into crm_logo(ImageFileName,Company,Description) values(@ImageFileName,@Company,@Description)", para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        /// <summary>
        /// function for edit business logo
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static int EditBusinessLogo(int ID, string ImageFileName, string Company, string Description)
        {
            int intReturnValue = 0;
            DataSet ds = new DataSet();
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ID", ID),
                    DBhelper.SetParameter("@ImageFileName", ImageFileName),
                    DBhelper.SetParameter("@Company", Company),
                    DBhelper.SetParameter("@Description", Description),
                };

                ds = DBhelper.ExecuteDataset("Select * From crm_logo Where ID <> @ID And  ImageFileName = @ImageFileName", para);

                if (ds.Tables[0].Rows.Count > 0)
                    intReturnValue = 0;
                else
                {
                    DBhelper.ExecuteNonQuery(" UPDATE crm_logo set ImageFileName = @ImageFileName,Company=@Company,Description=@Description where ID = @ID", para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        /// <summary>
        /// function for delete business logo
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool DeleteBusinessLogo(int ID)
        {
            bool flag = false;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@ID", ID),
                };

                DBhelper.ExecuteNonQuery("Delete from crm_logo where ID=@ID", para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }


        /// <summary>
        /// function to select business logo
        /// </summary>
        /// <param name="Company"></param>
        /// <param name="Company"></param>
        /// <returns></returns>

        public static DataTable SelectBusniessLogo(string Company)
        {
            DataTable dt = new DataTable();
            try
            {
                dt = DBhelper.ExecuteDataset(string.Format("select ImageFileName from crm_logo where Company='{0}'", Company)).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        /// <summary>
        /// function to select Theme according to company name
        /// </summary>
        /// <param name="Company"></param>
        /// <param name="Company"></param>
        /// <returns>ThemeName</returns>
        public static DataTable SelectTheme(string Company)
        {

            StringBuilder sql = new StringBuilder();
            DataTable dt = new DataTable();
            try
            {
                var para = new[]{
                    DBhelper.SetParameter("@p_Company",Company)
                };

                dt = DBhelper.ExecuteDataset("select Theme from crm_theme where CompanyName=@p_Company", para).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
